﻿@model List<Clirelmgt.Models.MetaTableField>
<script type="text/javascript">
    function FillLabel(ctl) {
        var str = ctl.id.split('_');
        var l = str[1];
        var labelCtl = document.getElementById("Labels_" + l);
        if (labelCtl.value == "") {
            labelCtl.value = ctl.value;
        }

        ctl.value = ctl.value.replace(' ', '');
    }

    function ChangeOptionAttrib(ctl) {
        var str = ctl.id.split('_');
        var l = str[1];
        var optionCtl = document.getElementById("Options_" + l);
        optionCtl.value = "";
        if (ctl.value == "11" || ctl.value == "12") {
            optionCtl.disabled = "";
        }
        else {
            optionCtl.disabled = "disabled";
        }

        var lengthCtl = document.getElementById("FieldLength_" + l);
        lengthCtl.value = "0";
        if (ctl.value == "1" || ctl.value == "2") {
            lengthCtl.disabled = "";
        }
        else {
            lengthCtl.disabled = "disabled";
        }
    }
</script>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
    <thead>
        <tr>
            <th>
                <input type="checkbox" class="no-margin" />
            </th>
            <th>Name
            </th>
            <th>Label
            </th>
            <th>Data Type - Options
            </th>
            <th>Data Length
            </th>
            <th>Not Null
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
            string dataTypes = "Key,Text,MultilineText,MaxText,Email,URL,Number,SmallNumber,Decimal,Bool,Date,Select,Table";
            string[] dataTypeList = dataTypes.Split(',');
            int loop = 0;
            foreach (var field in Model)
            {
                int counter = 0;
                bool ignoreFields = false;
                if (field.Name == "No" || field.Name == "Description")
                {
                    ignoreFields = true;
                }
            <tr>
                <td style="width: 30px">
                    @if (!ignoreFields)
                    {
                        <input type="checkbox" class="no-margin" id="SelectRow_@loop" name="SelectRow_@loop" />
                    }
                </td>
                <td>
                    @if (ignoreFields)
                    {
                        <input type="text" style="width:220px" id="Name_@loop" name="Name_@loop" readonly="true" data-val="true" data-val-required="Required!" value="@field.Name" />
                    }
                    else
                    {
                        <input type="text" style="width:220px" id="Name_@loop" name="Name_@loop" data-val="true" data-val-required="Required!" value="@field.Name" onblur="FillLabel(this);" />
                    }
                </td>
                <td>
                    <input type="text" style="width:250px" id="Labels_@loop" name="Labels_@loop" data-val="true" data-val-required="Required!" value="@field.Labels.Where(obj => obj.LangID == "en-US").ToList()[0].LabelText" />
                </td>
                <td>
                    <select id="DataType_@loop" name="DataType_@loop" style="width:120px" onchange="ChangeOptionAttrib(this);">

                        @foreach (string item in dataTypeList)
                        {
                            if (item == field.DataType.ToString())
                            {
                            <option value="@counter" selected="selected">@item</option>
                            }
                            else
                            {
                            <option value="@counter">@item</option> 
                            }
                            counter++;
                        }

                    </select>
                    &nbsp;
                    @if(field.DataType.ToString() == "Select" || field.DataType.ToString() == "Table")
                    {
                        <input type="text" style="width:150px;" id="Options_@loop" name="Options_@loop" data-val="true" data-val-required="Required!" value="@field.Options"/>
                    }
                    else
                    {
                        <input type="text" style="width:150px;" id="Options_@loop" name="Options_@loop" data-val="true" data-val-required="Required!" disabled="disabled" value="@field.Options"/>
                    }
                </td>
                <td>
                    @if(field.DataType.ToString() == "Text" || field.DataType.ToString() == "MultiText")
                    {
                    <input type="number" style="width:150px" id="FieldLength_@loop" name="FieldLength_@loop" data-val="true" data-val-required="Required!" value="@field.FieldLength" />
                    }
                    else
                    {
                    <input type="number" style="width:150px" id="FieldLength_@loop" name="FieldLength_@loop" data-val="true" data-val-required="Required!" value="@field.FieldLength" disabled="disabled"/>   
                    }
                </td>
                <td>
                    @if (field.NotNull)
                    {
                        <input type="checkbox" id="NotNull_@loop" name="NotNull_@loop" checked="checked" />
                    }
                    else
                    {
                        <input type="checkbox" id="NotNull_@loop" name="NotNull_@loop" />
                    }

                </td>
            </tr>
                    loop++;
            }


        }
    </tbody>
</table>

<script>
    //Alertify JS
    $ = function (id) {
        return document.getElementById(id);
    },
    reset = function () {
        $("toggleCSS").href = "../css/alertify.core.css";
        alertify.set({
            labels: {
                ok: "OK",
                cancel: "Cancel"
            },
            delay: 5000,
            buttonReverse: false,
            buttonFocus: "ok"
        });
    };

    </script>
    @if (TempData["alert"] != null && TempData["alert"] != "")
    {
        <script>
            alertify.alert("@TempData["alert"].ToString()");
        </script>
        TempData["alert"] = null;
    }